home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / hc2_x / tcprogud.sit / TC Prog Guide / card_59075.txt < prev    next >
Text File  |  1991-02-27  |  2KB  |  33 lines

  1. -- card: 59075 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 4755
  5. -- name: 
  6.  
  7.  
  8. -- part contents for background part 4
  9. ----- text -----
  10. A few comments about this example: the sizeof operator in ANSI C produces a value whose type is size_t.  However in Think C 4.0 its type is simply int.  Here this does not cause difficulties since the prototype for malloc() enforces the necessary type conversion.  Also note that a cast operator* is used to convert the void pointer returned by malloc to the desired type.  This is not strictly necessary since automatic type conversion will take place upon assignment.
  11.  
  12. Dynamic memory allocation is often used when the amount of memory needed by an application is not known before run-time.  The program may define a large array of pointers to data structures, but dynamically allocate the individual data structures pointed to by elements of the array only as needed:
  13.  
  14.     struct  personnel_rec  *person_array[1000];   /* doesn't yet allocate any 
  15.                .                                                                             personnel_rec's */
  16.                .
  17.     person_array[i] = malloc(sizeof (struct personnel_rec));  /* allocate ith element */
  18.  
  19. -- part contents for background part 7
  20. ----- text -----
  21. 199
  22.  
  23. -- part contents for background part 29
  24. ----- text -----
  25. 47191
  26.  
  27. -- part contents for background part 27
  28. ----- text -----
  29. Casts for type conversion
  30.  
  31. -- part contents for background part 20
  32. ----- text -----
  33. Casts for type conversion - p150